package br.com.unifebe.tcc.GUI;

import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.com.unifebe.tcc.componente.GUI.DynamicGUI;
import br.com.unifebe.tcc.componente.GUI.GUIUtils;
import br.com.unifebe.tcc.model.Usuario;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

@SuppressWarnings({"serial"})
public class UsuarioGUI extends DynamicGUI<Usuario>{

	private Usuario usuario = new Usuario();

	private JTextField editLogin;
	private JTextField editSenha;
	
	public UsuarioGUI() {
		this( null );
	}
	
	public UsuarioGUI( Usuario usuario ) {
		this( null, usuario, "Usuario" );
	}
	public UsuarioGUI( Component owner, Usuario usuario, String title ) {
		super();
		
		setTitle(title);
		setObjeto(usuario);
	}

	public JTextField getEditSenha(){
		if(editSenha == null){
			editSenha = new JTextField();
		}
		return editSenha;
	}

	public JTextField getEditLogin(){
		if(editLogin == null){
			editLogin = new JTextField();
		}
		return editLogin;
	}

	@Override
	public JPanel buildPanel() {
		int row = 25;
		int col = 5;

		PanelBuilder builder = new PanelBuilder( new FormLayout(
				GUIUtils.createArrayColSpec( col, true ),
				GUIUtils.createArrayRowSpec( row, true )));

		CellConstraints cc = new CellConstraints();
		int currentLine = 2;

		builder.addLabel("Login", 			cc.xywh(  2, currentLine,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
		builder.add(getEditLogin(), 		cc.xywh( 10, currentLine, 40, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
		currentLine += 2;

		builder.addLabel("Senha", 			cc.xywh(  2, currentLine,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
		builder.add(getEditSenha(), 		cc.xywh( 10, currentLine, 40, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
		currentLine += 2;
		
		builder.getPanel().setBorder(BorderFactory.createEtchedBorder());

		return builder.getPanel();
	}

	
	@Override
	public Usuario getObjeto() {

		usuario.setLogin( getEditLogin().getText().trim() );
		usuario.setSenha( getEditSenha().getText().trim() );
		
		return usuario;
	}

	@Override
	public void setObjeto(Usuario usuario) {
		if( usuario != null ){
			getEditLogin().setText( usuario.getLogin() );
			getEditSenha().setText( usuario.getSenha() );
		}
	}
	
}
